﻿using System;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Services;
using System.Xml.Serialization;
using Model.Progect;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using BLL.SysManage;
namespace HQWeb.Wbserver
{
    /// <summary>
    /// UpFile 的摘要说明
    /// </summary>
    [WebService(Name = "TRY", Namespace = "http://tempuri.org/", Description="尝试")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class UpFile : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        #region 文件上传
        [XmlInclude(typeof(T_PUB_FILE))]
        [WebMethod]
        public bool UpFileRsout(string[] paraStr, byte[] bsThumbnail)
        {
            string ThumbnailName = "";
            try
            {
                BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
                T_PUB_FILE fileInfo = new T_PUB_FILE();
                fileInfo.FILE_ID = paraStr[0] + "-" + paraStr[6];
                fileInfo.BRANCH_ID = Convert.ToInt32(paraStr[6]);
                string filename = "";
                string PreFilename = "";
                if (Convert.ToInt32(paraStr[3]) == 0)
                {
                    filename = paraStr[0].Substring(0, 36).ToString() + "." + paraStr[1].Substring(paraStr[1].LastIndexOf(".") + 1).ToLower();
                }
                else if (Convert.ToInt32(paraStr[3]) == 1)
                {
                    filename=paraStr[0].Substring(0, 36).ToString() + "." + paraStr[1].Substring(paraStr[1].LastIndexOf(".") + 1).ToLower();
                    PreFilename = paraStr[0].Substring(0, 36).ToString() + "." + paraStr[8].Substring(paraStr[8].LastIndexOf(".") + 1).ToLower();
                }
                fileInfo.FILE_NAME = filename;
                fileInfo.USERID = Convert.ToInt32(paraStr[4]);
                fileInfo.USERNAME = paraStr[5];
                fileInfo.FILE_TITEL = paraStr[2];
                fileInfo.PROJECTREAMRK = "1";
                fileInfo.TYPE = paraStr[3];
                if (Convert.ToInt32(paraStr[3]) == 0)
                {
                    string urlPreview = Server.MapPath("../Images//FileManage//") + paraStr[8];
                    if (System.IO.Directory.Exists(urlPreview))
                    {
                        return false;
                    }
                    int FileLength = bsThumbnail.Length;
                    ThumbnailName = filename;
                    string urlThumbnail = Server.MapPath("../Images//FileManage//") + filename;
                    if (System.IO.Directory.Exists(urlThumbnail))
                    {
                        return false;
                    }
                    MemoryStream m = new MemoryStream(bsThumbnail);
                    FileStream fs = new FileStream(urlThumbnail, FileMode.CreateNew, FileAccess.Write);
                    m.WriteTo(fs);
                    fs.Close();
                    fileInfo.URLThumbnail = "Images//FileManage//" + filename;
                }
                else if (Convert.ToInt32(paraStr[3]) == 1)
                {
                    string urlPreview = Server.MapPath("../Images//FileManage//") + paraStr[8];
                    if (System.IO.Directory.Exists(urlPreview))
                    {
                        return false;
                    }
                    int FileLength = bsThumbnail.Length;
                    ThumbnailName = PreFilename;
                    string urlThumbnail = Server.MapPath("../Images//FileManage//") + PreFilename;
                    if (System.IO.Directory.Exists(urlThumbnail))
                    {
                        return false;
                    }
                    MemoryStream m = new MemoryStream(bsThumbnail);
                    FileStream fs = new FileStream(urlThumbnail, FileMode.CreateNew, FileAccess.Write);
                    m.WriteTo(fs);
                    fs.Close();
                    fileInfo.URLThumbnail = "Images//FileManage//" + PreFilename; 
                }
                fileInfo.URLOriginal =  paraStr[7] + filename;
                fileInfo.UPTIME = DateTime.Now;
                fileInfo.ISFLAG = false;
                fileInfo.ISUSED = true;
                fileBll.Add(fileInfo);
            }
            catch
            {
                System.IO.Directory.Delete(Server.MapPath("..//Images//FileManage//") + paraStr[8], true);
                return false;
            }
            return true;
        }
        #endregion

        #region 获取分店文件列表
        [XmlInclude(typeof(T_PUB_FILE))]
        [WebMethod]
        public DataSet GetfileList(bool flag, int branchID)
        {
            DataSet filelist = new DataSet();
            BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
            if (flag)
            {
                filelist = fileBll.GetList("A.BRANCH_ID=" + branchID);
            }
            else
            {
                filelist = fileBll.GetList("A.BRANCH_ID!=" + branchID);
            }
            return filelist;
        }
        #endregion

        #region 删除文件
        [XmlInclude(typeof(T_PUB_FILE))]
        [WebMethod]
        public bool Delete(string fileID)
        {
            BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
            bool flag = fileBll.Delete(fileID);
            string strCachName = "Exhibition_" + Session["BRANCH_ID"].ToString();
            Universal.Caches.RemoveIdentify(strCachName);
            return flag;
        }
        #endregion

        #region 文件下载
        [XmlInclude(typeof(T_PUB_FILE))]
        [WebMethod]
        public bool DownFile(string[] paraStr)
        {
            string ThumbnailName = "";
            try
            {
                BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
                T_PUB_FILE fileInfo = new T_PUB_FILE();
                fileInfo.FILE_ID = paraStr[0] + "-" + paraStr[6];
                fileInfo.BRANCH_ID = Convert.ToInt32(paraStr[6]);
                string filename = paraStr[0].Substring(0, 36).ToString() + "." + paraStr[1].Substring(paraStr[1].LastIndexOf(".") + 1).ToLower();
                fileInfo.FILE_NAME = filename;
                fileInfo.USERID = Convert.ToInt32(paraStr[4]);
                fileInfo.USERNAME = paraStr[5];
                fileInfo.FILE_TITEL = paraStr[2];
                fileInfo.TYPE = paraStr[3];
                string OriginalVideo = paraStr[7] + filename;
                string OriginalImage = paraStr[7] + filename;
                if (Convert.ToInt32(paraStr[3]) == 1)
                {
                    fileInfo.URLOriginal = OriginalVideo;
                    fileInfo.URLThumbnail = "Images//FileManage//" + paraStr[8];
                }
                if (Convert.ToInt32(paraStr[3]) == 0)
                {
                    fileInfo.URLOriginal = OriginalImage;
                    fileInfo.URLThumbnail = "Images//FileManage//" + filename;
                }
                fileInfo.UPTIME = DateTime.Now;
                fileInfo.ISFLAG = false;//是否已经对文件进行设置
                fileInfo.ISUSED = true;//逻辑删除使用
                fileBll.Add(fileInfo);
            }
            catch
            {
                if (Convert.ToInt32(paraStr[3]) == 1)
                {
                    System.IO.Directory.Delete(Server.MapPath("..//Images//FileManage//") + paraStr[8], true);
                    return false;
                }
                if (Convert.ToInt32(paraStr[3]) == 0)
                {
                    System.IO.Directory.Delete(Server.MapPath("..//Images//FileManage//") + ThumbnailName, true);
                    return false;
                }
            }
            return true;
        }
        #endregion

        #region 通过文件编号获取文件信息
        [XmlInclude(typeof(T_PUB_FILE))]
        [WebMethod]
        public DataSet GetfileInfo(string fileid)
        {
            DataSet fileInfo = new DataSet();
            BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
            fileInfo = fileBll.GetList("FILE_ID='" + fileid + "'");
            return fileInfo;
        }
        #endregion

        #region 分店文件表中添加数据
        [XmlInclude(typeof(T_PUB_BRANCHFILE))]
        [WebMethod]
        public bool InsertFile(string fileguid, int branchID, bool ishead)
        {
            bool flag = false;
            BLL.Progect.T_PUB_BRANCHFILE branchfileBll = new BLL.Progect.T_PUB_BRANCHFILE();
            T_PUB_BRANCHFILE branchifilemodel = new T_PUB_BRANCHFILE();
            branchifilemodel.FILE_ID = fileguid + "-" + branchID; ;
            branchifilemodel.BRANCH_ID = branchID.ToString();
            flag = branchfileBll.Add(branchifilemodel);
            return flag;
        }
        #endregion

        #region 分店文件表中删除数据
        [XmlInclude(typeof(T_PUB_BRANCHFILE))]
        [WebMethod]
        public bool DeleteFile(string fileGUID, string branchID, bool ishead)
        {
            bool flag = false;
            BLL.Progect.T_PUB_BRANCHFILE branchfileBll = new BLL.Progect.T_PUB_BRANCHFILE();
            DataSet branchfile = new DataSet();
            branchfile = branchfileBll.GetList("FILE_ID='" + fileGUID + "'");
            if (branchfile.Tables[0].Rows.Count > 0)
            {
                int ID = Convert.ToInt32(branchfile.Tables[0].Rows[0]["ID"].ToString());
                flag = branchfileBll.Delete(ID, branchID, ishead);
            }
            else
            {
                flag = false;
            }
            return flag;
        }
        #endregion

        #region 分店文件表中修改数据
        [XmlInclude(typeof(T_PUB_BRANCHFILE))]
        [WebMethod]
        public bool UpdateFile(string fileGUID, int branchID)
        {
            bool flag = false;
            BLL.Progect.T_PUB_BRANCHFILE branchfileBll = new BLL.Progect.T_PUB_BRANCHFILE();
            DataSet branchfile = new DataSet();
            branchfile = branchfileBll.GetList("FILE_ID=" + fileGUID);
            int ID = Convert.ToInt32(branchfile.Tables[0].Columns["ID"].ToString());
            flag = branchfileBll.Update(ID, branchID);
            return flag;
        }
        #endregion

        public byte[] GenThumbnail(int width, int height, byte[] ImageByte)
        {
            Image imageFrom = null;
            try
            {
                if (ImageByte == null)
                    return null;
                MemoryStream ms = new System.IO.MemoryStream(ImageByte);
                imageFrom = Image.FromStream(ms);
                ms.Flush();
            }
            catch
            {
                return null;
            }
            if (imageFrom == null)
            {
                return null;
            }
            // 源图宽度及高度 
            int imageFromWidth = imageFrom.Width;
            int imageFromHeight = imageFrom.Height;
            // 生成的缩略图实际宽度及高度 
            int bitmapWidth = width;
            int bitmapHeight = height;
            // 生成的缩略图在上述"画布"上的位置 
            int X = 0;
            int Y = 0;
            // 根据源图及欲生成的缩略图尺寸,计算缩略图的实际尺寸及其在"画布"上的位置 
            if (bitmapHeight * imageFromWidth > bitmapWidth * imageFromHeight)
            {
                //bitmapHeight = imageFromHeight * width / imageFromWidth;
                Y = (height - bitmapHeight) / 2;
            }
            else
            {
                //bitmapWidth = imageFromWidth * height / imageFromHeight;
                X = (width - bitmapWidth) / 2;
            }
            // 创建画布 
            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            // 用白色清空    
            g.Clear(Color.White);
            // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。 
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
            // 指定高质量、低速度呈现。 
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            // 在指定位置并且按指定大小绘制指定的 Image 的指定部分。 
            g.DrawImage(imageFrom, new Rectangle(X, Y, bitmapWidth, bitmapHeight), new Rectangle(0, 0, imageFromWidth, imageFromHeight), GraphicsUnit.Pixel);
            try
            {
                byte[] bsThumbnailImage = ImageToStream(bmp);
                return bsThumbnailImage;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                //显示释放资源 
                imageFrom.Dispose();
                bmp.Dispose();
                g.Dispose();
            }
        }

        public byte[] ImageToStream(Bitmap bmp)
        {
            System.IO.MemoryStream bstream = new System.IO.MemoryStream();
            bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg);
            bmp.Dispose();
            byte[] byteReturn = bstream.ToArray();
            bstream.Close();
            return byteReturn;
        }

        #region 获取数据字典中的数据key-value
        [XmlInclude(typeof(T_PUB_DICTIONARY))]
        [WebMethod]
        public DataSet GetDictionary(int parentid)
        {
            DataSet Dictionaryds = new DataSet();
            BLL.SysManage.T_PUB_DICTIONARY DictionaryBll = new BLL.SysManage.T_PUB_DICTIONARY();
            Dictionaryds = DictionaryBll.GetList("PARENT_ID=" + parentid);
            return Dictionaryds;
        }
        #endregion

        #region 获取项目类别表中项目类别key-value
        [XmlInclude(typeof(T_PUB_PROJECTTYPE))]
        [WebMethod]
        public DataSet GetProType(int projectid)
        {
            DataSet Protypeds = new DataSet();
            BLL.SysManage.T_PUB_PROJECTTYPE ProjectBll = new BLL.SysManage.T_PUB_PROJECTTYPE();
            Protypeds = ProjectBll.GetList("PARENT_ID=" + projectid);
            return Protypeds;
        }
        #endregion

        [XmlInclude(typeof(T_PUB_FILE))]
        [WebMethod]
        #region 下载文件时绑定数据
        public DataSet GetFileDownList(int branchid)
        {
            BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
            DataSet filedown = fileBll.GetFileDownList("  A.FILE_ID=B.FILE_ID  and  A.BRANCH_ID=D.BRANCH_ID  and  A.BRANCH_ID!=" + branchid);
            return filedown;
        }
        #endregion

        [XmlInclude(typeof(T_PUB_FILE))]
        [WebMethod]
        #region 文件下载时属性同步
        public bool Synchronizaion(string fileguid, bool isfileattr, bool isfileproject, int branchfrom, int branchto)
        {
            BLL.Progect.T_PUB_FILE fileBll = new BLL.Progect.T_PUB_FILE();
            return fileBll.Synchronizaion(fileguid, isfileattr, isfileproject, branchfrom, branchto);
        }
        #endregion
    }
}
